package com.psm.controller;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.psm.interceptor.IndexInterceptor;
import com.psm.model.DataGrid;
import com.psm.model.SysPaCode;
import com.psm.model.SysUser;
import com.psm.util.MD5;

import org.apache.log4j.Logger;

import javax.security.sasl.SaslServer;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author 广州朴石茗文化发展有限公司
 * @Email pushiming@sina.cn
 */
@Before(IndexInterceptor.class)
public class CodeController extends BaseController<SysUser> {
    public Logger log=Logger.getLogger(getClass());

    public void index(){

        render("index.html");
    }


    public void listJson(){

        DataGrid dg = new DataGrid();

        List<Object> param = new ArrayList<Object>();
        StringBuffer where = new StringBuffer();
        /** 添加查询字段条件*/
        String sortName = this.getPara("sort");
        String sortOrder = this.getPara("order", "desc");

        String type = this.getPara("type","");
        String name = this.getPara("name", "");

        where.append(" where 1=1 ");


        if(!"".equals(name)){
            where.append(" and name like '%" + name + "%'");
        }

        if(!"".equals(type)){
            where.append(type);
        }else{
            dg.setRows(new ArrayList<Object>());
            dg.setTotal(0);

            renderJson(dg);

            return;
        }

        if (sortName != null) {
            where.append(" order by ");
            where.append(sortName);
            where.append(" ");
            where.append(sortOrder);
        }

        Page<Record> p = Db.paginate(this.getParaToInt("page", 1), this.getParaToInt("rows", 20), "select * ", "from " + SysPaCode.tableName + where.toString(), param.toArray());

        dg.setRows(p.getList());
        dg.setTotal(p.getTotalRow());

        renderJson(dg);

    }

    public void edit() {
        try {
            Long id = getParaToLong("id");
            SysPaCode code = SysPaCode.me.findById(id);
            this.setAttr("po",code);
        } catch (Exception e) {
            log.error("编辑异常", e);
        }
    }


    public void edit_save(){
        String id = getPara("id","");
        String name = getPara("name", "");

        SysPaCode code = SysPaCode.me.findById(id);

        code.set("name",name);
        code.update();

        this.rendJson_(1, "操作成功");
    }

    public void delete(){
        String id = getPara("id", "");

        SysPaCode.me.deleteById(id);

        this.rendJson_(1, "操作成功");
    }

    public void add(){

    }

    public void save(){
         String name = getPara("name","");
         String bus_table = getPara("bus_table","");
         String bus_field = getPara("bus_field","");

        SysPaCode code = new SysPaCode();
        code.set("name",name);
        code.set("bus_table",bus_table);
        code.set("bus_field",bus_field);

        code.save();
        this.rendJson_(1, "操作成功");
    }

}
